<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: ""
    },
    chartdata: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart(this.chartdata.xData, this.chartdata.statisticsYData)
    // })
  },
  watch: {
    chartdata: {//深度监听，可监听到对象、数组的变化
      handler(val, oldVal){  
        this.chartdata = val;  
        this.initChart(this.chartdata.xData, this.chartdata.statisticsYData)
      },
      deep:true //true 深度监听
     }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(xData, yData) {
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(xData, yData);
    },
    setOptions(xData, yData) {
      this.chart.setOption({
        title: { 
          text: this.title + "销售统计",
          left: 'center',
          textStyle:{
              //文字颜色
              color:'#000000',
              //字体风格,'normal','italic','oblique'
              fontStyle:'normal',
              //字体粗细 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
              fontWeight:'bold',
              //字体系列
              fontFamily:'sans-serif',
              //字体大小
      　　　　 fontSize:18
          }
        },
        xAxis: {
          data: xData
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 50,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        series: [{
          name: '销量', 
          smooth: true,
          type: 'line',
          data: yData.yAmountData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },{
          name: '金额', 
          itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: yData.yMoneyData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }]
      })
    }
  }
}
</script>
